package com.hrhx.duhongming.adapter.echarts;

import java.util.Map;

import com.github.abel533.echarts.code.SeriesType;
import com.github.abel533.echarts.data.PointData;
import com.github.abel533.echarts.json.GsonOption;
import com.hrhx.duhongming.adapter.EchartsAdapter;
import com.hrhx.duhongming.bean.EchartsConfig;

public class EchartsMapAdapter implements EchartsAdapter{
	
	public static final EchartsMapAdapter me = new EchartsMapAdapter();

	public GsonOption adapter(EchartsConfig echartsConfig, GsonOption gsonOption,
			Map<String, Map<String, Double>> data) {
		//EchartsMapAdapter mapAdapter = new EchartsMapAdapter();
		gsonOption = me.legendAdapter(gsonOption,data);
		gsonOption = me.seriesAdapter(echartsConfig,gsonOption,data);
		return gsonOption;
	}

	//legend
	public GsonOption legendAdapter(GsonOption gsonOption,Map<String, Map<String, Double>> data){
		if(data != null && data.size() != 0){
			for (String key : data.keySet()) {
				//图例组件
				gsonOption.legend().data(key);
			}
		}
		return gsonOption;
	}
	
	//数据处理
	public GsonOption seriesAdapter(EchartsConfig echartsConfig,GsonOption gsonOption,Map<String, Map<String, Double>> data){
		Integer max=0;
		Integer min=0;
		for(Object legendData : gsonOption.legend().data()){
			
			com.github.abel533.echarts.series.Map echartMap = new com.github.abel533.echarts.series.Map();	
			echartMap = echartMap.type(SeriesType.map)
					   			 .name((String)legendData)
					   			 .mapType("china");
			echartMap.label().normal().show(true);
			echartMap.label().emphasis().show(true);
			if(data != null && data.size() != 0){
				Map<String,Double> map = data.get(legendData);
				for (String key : map.keySet()) {
					if(map.get(key) > max){
						max = map.get(key).intValue();
					}
					if(min > map.get(key)){
						min = map.get(key).intValue();
					}
					echartMap.data(new PointData(key,map.get(key)));
				}
			}
			gsonOption.series(echartMap);
		}
		gsonOption.visualMap().get(0).min(min).max(max*gsonOption.legend().data().size());
		return gsonOption;
	}
}
